<script lang="ts" setup>
import {
  getOneByCondition,
  getReadonly,
  insertOne,
  reSetDialogModel,
  updateOne,
  urls
} from '@/index';

import type { AsmePersonExamContent, AsmePersonTrainCourse, DialogModel } from '@/index.d';
import type { FormInstance, FormRules } from 'element-plus';

const dialogModel = defineModel<DialogModel<AsmePersonExamContent>>('dialogModel', {
  required: true
});

const { insertForbidden, updateForbidden } = getReadonly(dialogModel.value);

const formData = reactive<AsmePersonExamContent>({
  id: 0,
  sequence: 0
});

const setFormData = async (res: AsmePersonExamContent) => {
  formData.id = res.id;
  formData.sequence = res.sequence;
  formData.method = res.method;
  formData.level = res.level;
  formData.examKind = res.examKind;
};

const reSetFormData = () => {
  reSetDialogModel(dialogModel);
  formData.id = 0;
};

watch(
  () => dialogModel.value.visible,
  value => {
    value && (formData.id = dialogModel.value.id as number);
    !value && reSetFormData();
  },
  {
    immediate: true
  }
);

watch(
  () => formData.id,
  async (value: number | undefined) => {
    if (value) {
      const res = await getOneByCondition<AsmePersonExamContent>(
        urls.asmePersonTrainCourse,
        'id',
        value
      );
      res && setFormData(res);
    }
  },
  {
    immediate: true
  }
);

const formDataRef = ref<FormInstance>();

interface FormDataRules {
  sequence: number;
  method: string;
  level: string;
  examKind: string;
}

const formDataRules = computed(() => {
  switch (dialogModel.value.type) {
    case 'view':
      return reactive({});

    case 'update':
      return reactive<FormRules<FormDataRules>>({
        sequence: [{ required: true, message: '序号不能为空', trigger: 'blur' }],
        method: [{ required: true, message: '方法不能为空', trigger: 'blur' }],
        level: [{ required: true, message: '级别不能为空', trigger: 'blur' }],
        examKind: [{ required: true, message: '考试种类不能为空', trigger: 'blur' }]
      });

    case 'insert':
      return reactive<FormRules<FormDataRules>>({
        sequence: [{ required: true, message: '序号不能为空', trigger: 'blur' }],
        method: [{ required: true, message: '方法不能为空', trigger: 'blur' }],
        level: [{ required: true, message: '级别不能为空', trigger: 'blur' }],
        examKind: [{ required: true, message: '考试种类不能为空', trigger: 'blur' }]
      });
  }
});

const saveFormData = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl
    .validate()
    .then(async valid => {
      if (valid) {
        if (dialogModel.value.id === 0) {
          const res = (await insertOne<AsmePersonExamContent>(
            urls.asmePersonExamContent,
            formData
          )) as AsmePersonExamContent;
          if (res) {
            setFormData(res);
            dialogModel.value.id = res.id as number;
            dialogModel.value.type = 'update';
            dialogModel.value.change = true;
          }
        } else {
          const tempFormData: AsmePersonExamContent = {
            id: formData.id,
            sequence: formData.sequence,
            method: formData.method,
            level: formData.level
          };

          const res = (await updateOne<AsmePersonExamContent>(
            urls.asmePersonExamContent,
            tempFormData
          )) as AsmePersonExamContent;

          if (res) {
            dialogModel.value.change = true;
            formData.sequence = res.sequence;
            formData.updater = res.updater;
            formData.updateTime = res.updateTime;
          }
        }
      }
    })
    .catch(res => console.log(res));
};
</script>
<template>
  <HqDialogDetail :dialog-model="dialogModel" @submit="saveFormData(formDataRef)">
    <template #default>
      <el-card shadow="hover">
        <template #header>基本信息</template>
        <template #default>
          <hq-form :model="formData" ref="formDataRef" :rules="formDataRules">
            <el-row :gutter="5">
              <hq-col>
                <el-form-item label="id" prop="id">
                  <el-input v-model.number="formData.id" readonly />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="序号" prop="sequence">
                  <el-input-number v-model.number="formData.sequence" readonly />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="方法" prop="method">
                  <el-select v-model="formData.method">
                    <HqNdeMethodOptions />
                  </el-select>
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="等级" prop="level">
                  <el-select v-model="formData.level">
                    <HqNdeMethodLevelOptions />
                  </el-select>
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="种类" prop="examKind">
                  <el-select v-model="formData.examKind">
                    <el-option
                      v-if="formData.level === 1 || formData.level === 2"
                      value="General"
                      label="General"
                    />
                    <el-option v-if="formData.level === 3" value="Basic" label="Basic" />
                    <el-option v-if="formData.level === 3" value="Method" label="Method" />
                    <el-option value="Specific" label="Specific" />
                  </el-select>
                </el-form-item>
              </hq-col>
            </el-row>
          </hq-form>
        </template>
      </el-card>
    </template>
  </HqDialogDetail>
</template>
<style lang="scss" scoped></style>
